package io.yunjiao.apijson.example;

import io.yunjiao.apijson.example.gson.CustomGsonCreator;
import io.yunjiao.apijson.gson.GsonCreator;
import io.yunjiao.apijson.spring.boot.autoconfigure.ApijsonSqlProperties;
import io.yunjiao.apijson.spring.boot.autoconfigure.condition.ApllicationCondition;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 通用配置
 *
 * @author yangyunjiao
 */
@Slf4j
@Conditional(ApllicationCondition.OnGson.class)
@Configuration
public class ExampleGsonAutoConfiguration {
    @PostConstruct
    public void postConstruct() {
        log.info("Example Gson Auto Configuration");
    }

    @Bean
    public DataSource DriverManagerDataSource() {
        log.debug("Configure Bean [Driver Manager DataSource]");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/apijson?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"); // 内存模式
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

    @Bean
    public GsonCreator customGsonCreator(DataSource dataSource,
                                         ApijsonSqlProperties sqlProperties) {
        log.debug("Configure Bean [Custom Gson Creator]");
        return new CustomGsonCreator(dataSource, sqlProperties);
    }
}
